package com.yfp.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.Client;
import com.yfp.operation.domain.ClientHealthLog;
import com.yfp.operation.domain.dto.ClientHealthInfoDTO;
import com.yfp.operation.domain.dto.ClientHealthListDTO;
import com.yfp.operation.domain.vo.ClientHealthInfoVO;
import com.yfp.operation.domain.vo.ClientHealthListVO;
import com.yfp.operation.enums.ClientHealthEnum;
import com.yfp.operation.mapper.ClientMapper;
import com.yfp.operation.service.ClientHealthLogService;
import com.yfp.operation.mapper.ClientHealthLogMapper;
import com.yfp.operation.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【client_health_log(客户健康餐饮记录)】的数据库操作Service实现
* @createDate 2025-02-11 15:29:26
*/
@Service
public class ClientHealthLogServiceImpl extends ServiceImpl<ClientHealthLogMapper, ClientHealthLog> implements ClientHealthLogService{

    @Resource
    private ClientHealthLogMapper clientHealthLogMapper;
    @Resource
    private ClientMapper clientMapper;


    /**
     * 用户热量档案列表
     * @param clientHealthListDTO
     * @return
     */
    @Override
    public List<ClientHealthListVO> clientHealthList(ClientHealthListDTO clientHealthListDTO) {
        // 用户热量档案列表
        List<ClientHealthListVO> list = clientHealthLogMapper.clientHealthList(clientHealthListDTO);
        return list;
    }


    /**
     * 补全
     * @param list
     * @return
     */
    @Override
    public List<ClientHealthListVO> clientHealthListComplement(List<ClientHealthListVO> list) {
        if (!CollUtil.isEmpty(list)) {
            List<String> clientIds = list.stream().map(item -> item.getClientId()).toList();
            // 根据客户id获取客户信息
            List<Client> clients = clientMapper.selectByUserIds(clientIds);
            Map<String, Client> map = clients.stream().collect(Collectors.toMap(Client::getId, Function.identity()));
            // 信息封装
            for (ClientHealthListVO clientHealthListVO : list) {
                if (map.containsKey(clientHealthListVO.getClientId())) {
                    Client client = map.get(clientHealthListVO.getClientId());
                    // 不拷贝生日
                    client.setBirthday(null);
                    BeanUtil.copyProperties(client, clientHealthListVO, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                }
                // 计算基础代谢
                getBMR(clientHealthListVO);
                // 计算日均摄入热量
                clientHealthListVO.setAverageKcal(BigDecimal.valueOf(0));
                if (clientHealthListVO.getDays() != 0) {
                    BigDecimal averageKcal = BigDecimal.valueOf(clientHealthListVO.getTotalKcal())
                            .divide(BigDecimal.valueOf(clientHealthListVO.getDays()), 0, RoundingMode.HALF_UP);
                    clientHealthListVO.setAverageKcal(averageKcal);
                }
            }
        }
        return list;
    }

    /**
     * 每餐饮食详情
     * @param clientHealthInfoDTO
     * @return
     */
    @Override
    public List<ClientHealthInfoVO> clientHealthInfo(ClientHealthInfoDTO clientHealthInfoDTO) {
        List<ClientHealthInfoVO> list = clientHealthLogMapper.clientHealthInfo(clientHealthInfoDTO);
        return list;
    }


    /**
     * 计算基础代谢
     * @param chVO
     * @return
     */
    public void getBMR(ClientHealthListVO chVO) {
        // 计算年龄
        Integer age = DateUtils.getAge(chVO.getBirthday());

        BigDecimal bmr = BigDecimal.valueOf(0);
        switch (chVO.getSex()) {
            case "男" :
                bmr = BigDecimal.valueOf(ClientHealthEnum.ManBasicParameter.getValue())
                        .add(BigDecimal.valueOf(ClientHealthEnum.ManWeightParameter.getValue()).multiply(chVO.getWeight()))
                        .add(BigDecimal.valueOf(ClientHealthEnum.ManHeightParameter.getValue()).multiply(chVO.getHeight()))
                        .subtract(BigDecimal.valueOf(ClientHealthEnum.ManAgeParameter.getValue()).multiply(BigDecimal.valueOf(age)))
                        .setScale(0, RoundingMode.HALF_UP); break;
            case "女" :
                bmr = BigDecimal.valueOf(ClientHealthEnum.WomanBasicParameter.getValue())
                        .add(BigDecimal.valueOf(ClientHealthEnum.WomanWeightParameter.getValue()).multiply(chVO.getWeight()))
                        .add(BigDecimal.valueOf(ClientHealthEnum.WomanHeightParameter.getValue()).multiply(chVO.getHeight()))
                        .subtract(BigDecimal.valueOf(ClientHealthEnum.WomanAgeParameter.getValue()).multiply(BigDecimal.valueOf(age)))
                        .setScale(0, RoundingMode.HALF_UP); break;
        }
        chVO.setBmr(bmr);
    }



}




